R Bootcamp, Module 6: Data manipulation with the tidyverse

August 2021, UC Berkeley

Chris Paciorek

Overview

It is often said that 80% of data analysis is spent on the process of cleaning and preparing the data. (Dasu and Johnson, 2003)

Thus before you can even get to doing any sort of sophisticated analysis or plotting, you’ll generally first need to:

  1. Manipulating data frames, e.g., filtering, summarizing, and conducting calculations across groups.
  2. Tidying data into the appropriate format

The tidyverse is a suite of packages designed specifically to help with both these steps; some of which we will be introducing in this module. These are by no means the only packages out there for data wrangling but they are increasingly popular for their readable, straightforward syntax and sensible default behaviors.

Data frame Manipulation using Base R functions

So far, you’ve seen the basics of manipulating data frames, e.g. subsetting, merging, and basic calculations. For instance, we can use base R functions to calculate summary statistics across groups of observations, e.g., the mean GDP per capita within each region:

mean(gap[gap$continent == "Africa", "gdpPercap"])
## Error in mean(gap[gap$continent == "Africa", "gdpPercap"]): object 'gap' not found
mean(gap[gap$continent == "Americas", "gdpPercap"])
## Error in mean(gap[gap$continent == "Americas", "gdpPercap"]): object 'gap' not found
mean(gap[gap$continent == "Asia", "gdpPercap"])
## Error in mean(gap[gap$continent == "Asia", "gdpPercap"]): object 'gap' not found

But this isn’t ideal because it involves a fair bit of repetition. Repeating yourself will cost you time, both now and later, and potentially introduce hard to find bugs.

Data frame manipulation using dplyr

Luckily, the dplyr package provides a number of very useful functions for manipulating data frames. These functions will save you time and hassle by reducing repetition, and will help to make your code more human-readable (trust me: your future self and others might thank you!)

Here we’re going to cover 6 of the most commonly used functions as well as using pipes (%>%) to combine them.

  1. select()
  2. filter()
  3. group_by()
  4. summarize()
  5. mutate()
  6. arrange()

If you have have not installed this package earlier, please do so now:

# NOT run
install.packages('dplyr')

Now let’s load the package:

library(dplyr)

dplyr::select

Imagine that we just received the gapminder dataset, but are only interested in a few variables in it. The select() function can help us to keep only the columns corresponding to variables we select.

year_country_gdp_dplyr <- select(gap, year, country, gdpPercap)
## Error in select(gap, year, country, gdpPercap): object 'gap' not found
head(year_country_gdp_dplyr)
## Error in head(year_country_gdp_dplyr): object 'year_country_gdp_dplyr' not found

If we open up year_country_gdp, we’ll see that it only contains the year, country and gdpPercap. This is equivalent to the base R subsetting function:

year_country_gdp_base <- gap[,c("year", "country", "gdpPercap")]
## Error in eval(expr, envir, enclos): object 'gap' not found
head(year_country_gdp_base)
## Error in head(year_country_gdp_base): object 'year_country_gdp_base' not found

We can even check that these two data frames are equivalent:

# checking equivalence: TRUE indicates an exact match between these objects
all.equal(year_country_gdp_dplyr, year_country_gdp_base)
## Error in all.equal(year_country_gdp_dplyr, year_country_gdp_base): object 'year_country_gdp_dplyr' not found

But, as we will see, dplyr makes for much more readable, efficient code because of its pipe operator.

piping with dplyr

Above, we used what’s called “normal” grammar, but the strengths of dplyr lie in combining several functions using pipes.

In typical base R code, a simple operation might be written like:

# NOT run
cupcakes <- bake(pour(mix(ingredients)))

A computer has no trouble understanding this and your cupcakes will be made just fine but a person has to read right to left to understand the order of operations - the opposite of how most western languages are read - making it harder to understand what is being done!

To be more readable without pipes, we might break up this code into intermediate objects…

## NOT run
batter <- mix(ingredients)
muffin_tin <- pour(batter)
cupcakes <- bake(muffin_tin)

but this can clutter our environment with a lot of variables that aren’t very useful to us, and often are named very similar things (e.g. step, step1, step2…) which can lead to confusion and those hard-to-track-down bugs.

Enter the pipe…

The pipe makes it easier to read code because it lays out the operations left to right so each line can be read like a line of a recipe for the perfect data frame!

Pipes take the input on the left side of the %>% symbol and pass it in as the first argument to the function on the right side.

With pipes, our cupcake example might be written like:

## NOT run
cupcakes <- ingredients %>% 
  mix() %>% 
  pour() %>% 
  bake()

Pro Tip: In RStudio the hotkey for the pipe is Ctrl + Shift + M.

R Update: If you have R 4.1.0 or later, you can use |> from base R without needing to load dplyr.

select & Pipe (%>%)

Since the pipe grammar is unlike anything we’ve seen in R before, let’s repeat what we did above with the gapminder dataset using pipes:

year_country_gdp <- gap %>% select(year, country, gdpPercap)
## Error in select(., year, country, gdpPercap): object 'gap' not found

First, we summon the gapminder data frame and pass it on to the next step using the pipe symbol %>%. The second step is the select() function. In this case we don’t specify which data object we use in the call to select() since we’ve piped it in.

Fun Fact: There is a good chance you have encountered pipes before in the shell. In R, a pipe symbol is %>% while in the shell it is |. But the concept is the same!

Selecting columns: quick quiz

POLL 6A: Which of these will produce an error? (Assume ‘continent’ is a column in the data frame but not a stand-alone variable.)

  1. gap[ , ’continent’]
  2. gap[ , continent]
  3. gap %>% select(gap, continent)
  4. gap %>% select(gap, ‘continent’)
  5. gap$continent
  6. gap$“continent”
  7. gap[[continent]]
  8. gap[[‘continent’]]

dplyr::filter

Now let’s say we’re only interested in African countries. We can combine select and filter to select only the observations where continent is Africa.

year_country_gdp_africa <- gap %>%
    filter(continent == "Africa") %>%
    select(year,country,gdpPercap)
## Error in filter(., continent == "Africa"): object 'gap' not found

As with last time, first we pass the gapminder data frame to the filter() function, then we pass the filtered version of the gapminder data frame to the select() function.

To clarify, both the select and filter functions subsets the data frame. The difference is that select extracts certain columns, while filter extracts certain rows.

Note: The order of operations is very important in this case. If we used ‘select’ first, filter would not be able to find the variable continent since we would have removed it in the previous step.

dplyr Calculations Across Groups

A common task you’ll encounter when working with data is running calculations on different groups within the data. For instance, what if we wanted to calculate the mean GDP per capita for each continent?

In base R, you would have to run the mean() function for each subset of data:

mean(gap$gdpPercap[gap$continent == "Africa"])
## Error in mean(gap$gdpPercap[gap$continent == "Africa"]): object 'gap' not found
mean(gap$gdpPercap[gap$continent == "Americas"])
## Error in mean(gap$gdpPercap[gap$continent == "Americas"]): object 'gap' not found
mean(gap$gdpPercap[gap$continent == "Asia"])
## Error in mean(gap$gdpPercap[gap$continent == "Asia"]): object 'gap' not found
mean(gap$gdpPercap[gap$continent == "Europe"])
## Error in mean(gap$gdpPercap[gap$continent == "Europe"]): object 'gap' not found
mean(gap$gdpPercap[gap$continent == "Oceania"])
## Error in mean(gap$gdpPercap[gap$continent == "Oceania"]): object 'gap' not found

That’s a lot of repetition! To make matters worse, what if we wanted to add these values to our original data frame as a new column? We would have to write something like this:

gap$mean.continent.GDP <- NA
## Error in gap$mean.continent.GDP <- NA: object 'gap' not found
gap$mean.continent.GDP[gap$continent == "Africa"] <- mean(gap$gdpPercap[gap$continent == "Africa"])
## Error in mean(gap$gdpPercap[gap$continent == "Africa"]): object 'gap' not found
gap$mean.continent.GDP[gap$continent == "Americas"] <- mean(gap$gdpPercap[gap$continent == "Americas"])
## Error in mean(gap$gdpPercap[gap$continent == "Americas"]): object 'gap' not found
gap$mean.continent.GDP[gap$continent == "Asia"] <- mean(gap$gdpPercap[gap$continent == "Asia"])
## Error in mean(gap$gdpPercap[gap$continent == "Asia"]): object 'gap' not found
gap$mean.continent.GDP[gap$continent == "Europe"] <- mean(gap$gdpPercap[gap$continent == "Europe"])
## Error in mean(gap$gdpPercap[gap$continent == "Europe"]): object 'gap' not found
gap$mean.continent.GDP[gap$continent == "Oceania"] <- mean(gap$gdpPercap[gap$continent == "Oceania"])
## Error in mean(gap$gdpPercap[gap$continent == "Oceania"]): object 'gap' not found

You can see how this can get pretty tedious, especially if we want to calculate more complicated or refined statistics. We could use loops or apply functions, but these can be difficult, slow, or error-prone.

dplyr split-apply-combine

The abstract problem we’re encountering here is know as “split-apply-combine”:

We want to split our data into groups (in this case continents), apply some calculations on each group, then combine the results together afterwards.

Module 4 gave some ways to do split-apply-combine type operations using the apply family of functions, but dplyr offers a cleaner, more straight-forward solution to this problem specifically for data frames.

# Want to remove the column we just made? -- there are two easy ways!
gap <- gap %>% select(-mean.continent.GDP) # drop a column with - 
## Error in select(., -mean.continent.GDP): object 'gap' not found
# OR
gap$mean.continent.GDP <- NULL
## Error in gap$mean.continent.GDP <- NULL: object 'gap' not found

dplyr::group_by

We’ve already seen how filter() can help us select observations that meet certain criteria (in the above: continent == "Europe"). More helpful, however, is the group_by() function, which will essentially use every unique criteria that we could have used in filter().

A grouped_df can be thought of as a list where each item in the list is a data.frame which contains only the rows that correspond to the a particular value continent (at least in the example above).

dplyr::summarize

group_by() on its own is not particularly interesting. It’s much more exciting used in conjunction with the summarize() function. This will allow use to create new variable(s) by applying transformations to variables in each of the continent-specific data frames. In other words, using the group_by() function, we split our original data frame into multiple pieces, which we then apply summary functions to (e.g. mean() or sd()) within summarize(). The output is a new data frame reduced in size, with one row per group.

gdp_bycontinents <- gap %>%
    group_by(continent) %>%
    summarize(mean_gdpPercap = mean(gdpPercap))
## Error in group_by(., continent): object 'gap' not found
head(gdp_bycontinents)
## Error in head(gdp_bycontinents): object 'gdp_bycontinents' not found

That allowed us to calculate the mean gdpPercap for each continent. But it gets even better – the function group_by() allows us to group by multiple variables. Let’s group by year and continent.

gdp_bycontinents_byyear <- gap %>%
    group_by(continent, year) %>%
    summarize(mean_gdpPercap = mean(gdpPercap))
## Error in group_by(., continent, year): object 'gap' not found
head(gdp_bycontinents_byyear)
## Error in head(gdp_bycontinents_byyear): object 'gdp_bycontinents_byyear' not found

That is already quite powerful, but it gets even better! You’re not limited to defining 1 new variable in summarize().

gdp_pop_bycontinents_byyear <- gap %>%
    group_by(continent, year) %>%
    summarize(mean_gdpPercap = mean(gdpPercap),
              sd_gdpPercap = sd(gdpPercap),
              mean_pop = mean(pop),
              sd_pop = sd(pop))
## Error in group_by(., continent, year): object 'gap' not found
head(gdp_pop_bycontinents_byyear)
## Error in head(gdp_pop_bycontinents_byyear): object 'gdp_pop_bycontinents_byyear' not found

dplyr::mutate

What if we wanted to add these values to our original data frame instead of creating a new object? For this, we can use the mutate() function, which is similar to summarize() except it creates new variables in the same data frame that you pass into it.

gap_with_extra_vars <- gap %>%
    group_by(continent, year) %>%
    mutate(mean_gdpPercap = mean(gdpPercap),
              sd_gdpPercap = sd(gdpPercap),
              mean_pop = mean(pop),
              sd_pop = sd(pop))
## Error in group_by(., continent, year): object 'gap' not found
head(gap_with_extra_vars)
## Error in head(gap_with_extra_vars): object 'gap_with_extra_vars' not found

We can use also use mutate() to create new variables prior to (or even after) summarizing information. Note that mutate() does not need to operate on grouped data and it can do element-wise transformations.

gdp_pop_bycontinents_byyear <- gap %>%
    mutate(gdp_billion = gdpPercap*pop/10^9) %>%
    group_by(continent, year) %>%
    summarize(mean_gdpPercap = mean(gdpPercap),
              sd_gdpPercap = sd(gdpPercap),
              mean_pop = mean(pop),
              sd_pop = sd(pop),
              mean_gdp_billion = mean(gdp_billion),
              sd_gdp_billion = sd(gdp_billion))
## Error in mutate(., gdp_billion = gdpPercap * pop/10^9): object 'gap' not found
head(gdp_pop_bycontinents_byyear)
## Error in head(gdp_pop_bycontinents_byyear): object 'gdp_pop_bycontinents_byyear' not found

mutate vs. summarize

It can be confusing to decide whether to use mutate or summarize. The key distinction is whether you want the output to have one row for each group or one row for each row in the original data frame:

Note that if you use an aggregation function such as mean() within mutate() without using groupby(), you’ll simply do the summary over all the rows of the input data frame.

And if you use an aggregation function such as mean() within summarize() without using groupby(), you’ll simply create an output data frame with one row (i.e., the whole input data frame is a single group).

mutate vs. summarize: quick quiz

POLL 6B: Which of these has the same number of rows as the original gap dataframe?

  1. gap %>% group_by(continent) %>% mutate(mean_gdp = mean(gdpPercap))
  2. gap %>% group_by(continent) %>% summarize(mean_gdp = mean(gdpPercap))
  3. gap %>% group_by(continent) %>% summarize(mean_gdp = mean(gdpPercap)) %>% mutate(mean_gdpBillion = mean_gdp / 1e9)

dplyr::arrange

As a last step, let’s say we want to sort the rows in our data frame according to values in a certain column. We can use the arrange() function to do this. For instance, let’s organize our rows by year (recent first), and then by continent.

gap_with_extra_vars <- gap %>%
    group_by(continent, year) %>%
    mutate(mean_gdpPercap = mean(gdpPercap),
              sd_gdpPercap = sd(gdpPercap),
              mean_pop = mean(pop),
              sd_pop = sd(pop)) %>%
    arrange(desc(year), continent) # `desc()` puts things ins descending order
## Error in group_by(., continent, year): object 'gap' not found
head(gap_with_extra_vars)
## Error in head(gap_with_extra_vars): object 'gap_with_extra_vars' not found

dplyr Take-aways

# without pipes:
gap_with_extra_vars <- arrange(
    mutate(
      group_by(gap, continent, year),
      mean_gdpPercap = mean(gdpPercap)
      ),
    desc(year), continent)
## Error in group_by(gap, continent, year): object 'gap' not found

dplyr and “non-standard evaluation”

You may run across the term “non-standard evaluation”. The use of data frame variables without quotes around them is an example of this.

Why is this strange?

gap %>% select(continent, year) %>% tail()

Compare it to:

gap[ , c('continent', 'year')]
gap[ , continent]

Because continent and year are not variables our current environment! dplyr does some fancy stuff behind the scenes to save us from typing the quotes.

This is fine if you have a data analysis workflow but if you want to write a function that, for example, selects an arbitrary set of columns, you’ll run into trouble.

## here's a helper function that computes the mean of a variable, stratifying by a grouping variable
grouped_mean <- function(data, group_var, summary_var) {
  data %>%
    group_by(group_var) %>%
    summarise(mean = mean(summary_var))
}
gap %>% grouped_mean(continent, lifeExp)
gap %>% grouped_mean('continent', 'lifeExp')

See the rlang or seplyr packages for how one can deal with this problem in this context of using functions.

Tidying Data

Even before we conduct analysis or calculations, we need to put our data into the correct format. The goal here is to rearrange a messy dataset into one that is tidy

The two most important properties of tidy data are:

  1. Each column is a variable.
  2. Each row is an observation.

Tidy data is easier to work with, because you have a consistent way of referring to variables (as column names) and observations (as row indices). It then becomes easy to manipulate, visualize, and model.

For more on the concept of tidy data, read Hadley Wickham’s paper here

Wide vs. Long Formats

“Tidy datasets are all alike but every messy dataset is messy in its own way.” – Hadley Wickham

Tabular datasets can be arranged in many ways. For instance, consider the data below. Each data set displays information on heart rate observed in individuals across 3 different time periods. But the data are organized differently in each table.

wide <- data.frame(
  name = c("Wilbur", "Petunia", "Gregory"),
  time1 = c(67, 80, 64),
  time2 = c(56, 90, 50),
  time3 = c(70, 67, 101)
)
wide
##      name time1 time2 time3
## 1  Wilbur    67    56    70
## 2 Petunia    80    90    67
## 3 Gregory    64    50   101
long <- data.frame(
  name = c("Wilbur", "Petunia", "Gregory", "Wilbur", "Petunia", "Gregory", "Wilbur", "Petunia", "Gregory"),
  time = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
  heartrate = c(67, 80, 64, 56, 90, 50, 70, 67, 10)
)
long
##      name time heartrate
## 1  Wilbur    1        67
## 2 Petunia    1        80
## 3 Gregory    1        64
## 4  Wilbur    2        56
## 5 Petunia    2        90
## 6 Gregory    2        50
## 7  Wilbur    3        70
## 8 Petunia    3        67
## 9 Gregory    3        10

POLL 6C: Which of the ‘wide’ and ‘long’ objects do you prefer in terms of how the heartrate ‘data’ are formatted?

  1. wide
  2. long
  3. I’d format it some other way

Question: Which one of these do you think is the tidy format?

Answer: The first data frame (the “wide” one) would not be considered tidy because values (i.e., heartrate) are spread across multiple columns.

We often refer to these different structures as “long” vs. “wide” formats. In the “long” format, you usually have 1 column for the observed variable and the other columns are ID variables.

For the “wide” format each row is often a site/subject/patient and you have multiple observation variables containing the same type of data. These can be either repeated observations over time, or observation of multiple variables (or a mix of both). In the above case, we had the same kind of data (heart rate) entered across 3 different columns, corresponding to three different time periods.

You may find data input may be simpler and some programs/functions may prefer the “wide” format. However, many of R’s functions have been designed assuming you have “long” format data.

Tidying the Gapminder Data

Lets look at the structure of our original gapminder data frame:

head(gap)
## Error in head(gap): object 'gap' not found

Question: Is this data frame wide or long?

Answer: This data frame is somewhere in between the purely ‘long’ and ‘wide’ formats. We have 3 “ID variables” (continent, country, year) and 3 “Observation variables” (pop, lifeExp, gdpPercap).

Despite not having ALL observations in 1 column, this intermediate format makes sense given that all 3 observation variables have different units. As we have seen, many of the functions in R are often vector based, and you usually do not want to do mathematical operations on values with different units.

On the other hand, there are some instances in which a purely long or wide format is ideal (e.g. plotting). Likewise, sometimes you’ll get data on your desk that is poorly organized, and you’ll need to reshape it.

tidyr

Thankfully, the tidyr package will help you efficiently transform your data regardless of original format.

# Install the "tidyr" package (only necessary one time)
# install.packages("tidyr") # Not Run

# Load the "tidyr" package (necessary every new R session)
library(tidyr)

tidyr::pivot_longer

Until now, we’ve been using the nicely formatted original gapminder data set. This data set is not quite wide and not quite long – it’s something in the middle, but “real” data (i.e., our own research data) will never be so well organized. Here let’s start with the wide format version of the gapminder data set.

gap_wide <- read.csv("../data/gapminder_wide.csv", stringsAsFactors = FALSE)
## Warning in file(file, "rt"): cannot open file '../data/gapminder_wide.csv': No
## such file or directory
## Error in file(file, "rt"): cannot open the connection
head(gap_wide)
## Error in head(gap_wide): object 'gap_wide' not found

The first step towards getting our nice intermediate data format is to first convert from the wide to the long format. The function pivot_longer() will ‘gather’ the observation variables into a single variable. This is sometimes called “melting” your data, because it melts the table from wide to long. Those data will be melted into two variables: one for the variable names, and the other for the variable values.

gap_long <- gap_wide %>% pivot_longer(gdpPercap_1952:pop_2007)
## Error in pivot_longer(., gdpPercap_1952:pop_2007): object 'gap_wide' not found
head(gap_long)
## Error in head(gap_long): object 'gap_long' not found

Formerly one used the function gather to do this, but many people found it not to be intuitive to use.

tidyr::select

If there are a lot of columns or they’re named in a consistent pattern, we might not want to select them using the column numbers. It’d be easier to use some information contained in the names themselves. We can select variables using:

See the select() function in dplyr for more options.

For instance, here we do the same gather operation with (1) the starts_with function, and (2) the - operator:

# with the starts_with() function
gap_long <- gap_wide %>%
    pivot_longer(c(starts_with('pop'), starts_with('lifeExp'), starts_with('gdpPercap')))
## Error in pivot_longer(., c(starts_with("pop"), starts_with("lifeExp"), : object 'gap_wide' not found
head(gap_long)
## Error in head(gap_long): object 'gap_long' not found
# with the - operator
gap_long <- gap_wide %>%
  pivot_longer(c(-continent, -country))
## Error in pivot_longer(., c(-continent, -country)): object 'gap_wide' not found
head(gap_long)
## Error in head(gap_long): object 'gap_long' not found

However you choose to do it, notice that the output collapses all of the measure variables into two columns: one containing new ID variable, the other containing the observation value for that row.

tidyr::separate

You’ll notice that in our long dataset, obstype_year actually contains 2 pieces of information, the observation type (pop, lifeExp, or gdpPercap) and the year.

We can use the separate() function to split the character strings into multiple variables:

gap_long_sep <- gap_long %>%
  separate(obstype_year, into = c('obs_type','year'), sep = "_") %>%
  mutate(year = as.integer(year))
## Error in separate(., obstype_year, into = c("obs_type", "year"), sep = "_"): object 'gap_long' not found
head(gap_long_sep)
## Error in head(gap_long_sep): object 'gap_long_sep' not found

If you didn’t use tidyr to do this, you’d have to use the strsplit function and use multiple lines of code to replace the column in gap_long with two new columns. This solution is much cleaner.

tidyr::pivot_wider

The opposite of pivot_longer() is pivot_wider(). It spreads our observation variables back out to make a wider table. We can use this function to spread our gap_long() to the original “medium” format.

gap_medium <- gap_long_sep %>%
  pivot_wider(names_from = obs_type, values_from = value)
## Error in pivot_wider(., names_from = obs_type, values_from = value): object 'gap_long_sep' not found
head(gap_medium)
## Error in head(gap_medium): object 'gap_medium' not found

Formerly one used the function spread to do this, but many people found it not to be intuitive to use.

All we need is some quick fixes to make this dataset identical to the original gap dataset:

gap <- read.csv("../data/gapminder-FiveYearData.csv")
head(gap_medium)
## Error in head(gap_medium): object 'gap_medium' not found
head(gap)
##       country year      pop continent lifeExp gdpPercap
## 1 Afghanistan 1952  8425333      Asia  28.801  779.4453
## 2 Afghanistan 1957  9240934      Asia  30.332  820.8530
## 3 Afghanistan 1962 10267083      Asia  31.997  853.1007
## 4 Afghanistan 1967 11537966      Asia  34.020  836.1971
## 5 Afghanistan 1972 13079460      Asia  36.088  739.9811
## 6 Afghanistan 1977 14880372      Asia  38.438  786.1134
# rearrange columns
gap_medium <- gap_medium[,names(gap)]
## Error in eval(expr, envir, enclos): object 'gap_medium' not found
head(gap_medium)
## Error in head(gap_medium): object 'gap_medium' not found
# arrange by country, continent, and year
gap_medium <- gap_medium %>%
  arrange(country, continent, year)
## Error in arrange(., country, continent, year): object 'gap_medium' not found
head(gap_medium)
## Error in head(gap_medium): object 'gap_medium' not found

Extra Resources

dplyr and tidyr have many more functions to help you wrangle and manipulate your data. See the Data Wrangling Cheat Sheet for more.

There are some other useful packages in the tidyverse:

Pro Tip: To install and load the core tidyverse packages (includes tidyr, dplyr, and ggplot2, among others), try:

## NOT run
install.packages("tidyverse")
library(tidyverse)

Breakout

dplyr

  1. Use dplyr to create a data frame containing the median lifeExp for each continent

  2. Use dplyr to add a column to the gapminder dataset that contains the total population of the continent of each observation in a given year. For example, if the first observation is Afghanistan in 1952, the new column would contain the population of Asia in 1952.

  3. Use dplyr to add a column called gdpPercap_diff that contains the difference between the observation’s gdpPercap and the mean gdpPercap of the continent in that year. Arrange the data frame by the column you just created, in descending order (so that the relatively richest country/years are listed first)

tidyr

  1. Subset the results from question #3 to select only the country, year, and gdpPercap_diff columns. Use tidyr put it in wide format so that countries are rows and years are columns.

Hint: you’ll probably see a message about a missing grouping variable. If you don’t want continent included, you can pass the output of problem 3 through ungroup() to get rid of the continent information.